from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver


class AcceptTest:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
    def AcceptMsgTest(self):
        #现在是zhangsan的主页
        wait = WebDriverWait(self.driver, 20)
        # 一开始进入界面时数据还未加载，等待加载完成后代码继续运行
        wait.until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#session-list > li:nth-child(7)"))
        )

        # -------------------通过会话列表，打开与 lisi 的聊天窗口--------------
        # 点击会话列表
        session_list = wait.until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR, "body > div.client-container > div > div.left > div.tab > div.tab-session"))
        )
        session_list.click()
        # 点击与 lisi 的会话
        lisi_session = wait.until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//*[@id=\"session-list\"]/li[1]/h3"))
        )
        lisi_session.click()
        print(f'session:{lisi_session.text}')
        # 查找消息
        msg = wait.until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body > div.client-container > div > div.right > div.message-show > div:nth-child(4) > div > p"))
        )
        print(f'收到李四的消息：{msg.text}')